using System.IO;
using System.Collections.Generic;
using UnityEngine;
using System.Text.RegularExpressions;
/// <summary>
/// UnityString扩展
/// </summary>
public static class HFUnityStringExtendEngine
{
    #region TransPathSeparatorCharToUnityChar 转换路径分隔符为Unity格式
    /// <summary>
    /// 转换路径分隔符为Unity格式缓存
    /// </summary>
    static Dictionary<int, string> mDicTransPathSeparatorCharToUnityChar = new Dictionary<int, string>();
    /// <summary>
    /// 转换路径分隔符为Popup格式缓存
    /// </summary>
    static Dictionary<int, string> mDicTransPathSeparatorCharToPopupChar = new Dictionary<int, string>();
    /// <summary>
    /// 转换路径分隔符为Unity格式
    /// "\"转换成"/"
    /// </summary>
    /// <param name="_path">路径</param>
    /// <returns>转换后路径</returns>
    public static string HFTransPathSeparatorCharToUnityChar(this string _path)
    {
        int key = string.IsNullOrEmpty(_path) ? 0 : _path.HFUniqueHashCode();
        if (!mDicTransPathSeparatorCharToUnityChar.ContainsKey(key))
        {
            mDicTransPathSeparatorCharToUnityChar.Add(key, _path.Replace(@"\", "/"));
        }
        return mDicTransPathSeparatorCharToUnityChar[key];
    }


    /// <summary>
    /// 转换路径分隔符为Window格式
    /// "/"转换成"\"
    /// </summary>
    /// <param name="_path">路径</param>
    /// <returns>转换后路径</returns>
    public static string HFTransPathSeparatorCharToWindowChar(this string _path)
    {
        int key = string.IsNullOrEmpty(_path) ? 0 : _path.HFUniqueHashCode();
        if (!mDicTransPathSeparatorCharToPopupChar.ContainsKey(key))
        {
            mDicTransPathSeparatorCharToPopupChar.Add(key, _path.Replace("/", @"\"));
        }
        return mDicTransPathSeparatorCharToPopupChar[key];
    }
    /// <summary>
    /// 组合路径
    /// </summary>
    /// <param name="_src">源路径</param>
    /// <param name="_concat">要组合的路径</param>
    /// <returns>组合后的路径</returns>
    public static string HFCombinePath(this string _src, string _concat)
    {
        if (string.IsNullOrEmpty(_src))
        {
            _src = _concat;
        }
        else
        {
            if (_src.EndsWith("/") || _src.EndsWith(@"\"))
            {
                _src = Path.Combine(_src, _concat);
            }
            else
            {
                _src = Path.Combine(_src + Path.DirectorySeparatorChar, _concat);
            }
        }
        return _src;
    }
    #endregion

    #region OnlyCNUAndOtherReplaceU 仅保留【字符、数字、下划线】其他字符替换为下划线
    /// <summary>
    /// 仅保留【字符、数字、下划线】
    /// </summary>
    const string mcIllegalOnlyCNUAndOtherReplaceU = @"[^\w\d_]";
    /// <summary>
    /// 仅保留【字符、数字、下划线】其他字符替换为下划线
    /// </summary>
    /// <param name="_string">要转换的字符</param>
    /// <returns>字符</returns>
    public static string HFOnlyCNUAndOtherReplaceU(this string _string)
    {
        return string.IsNullOrEmpty(_string) ? "" : Regex.Replace(_string, mcIllegalOnlyCNUAndOtherReplaceU, "_");
    }
    #endregion

    #region OnlyCNUSAndOtherReplaceU 仅保留【字符、数字、下划线、斜杠】其他字符替换为下划线
    /// <summary>
    /// 仅保留【字符、数字、下划线、斜杠】
    /// </summary>
    const string mcIllegalOnlyCNUSAndOtherReplaceU = @"[^\w\d_/\\]";
    /// <summary>
    /// 仅保留【字符、数字、下划线、斜杠】其他字符替换为下划线
    /// </summary>
    /// <param name="_string">要转换的字符</param>
    /// <returns>字符</returns>
    public static string HFOnlyCNUSAndOtherReplaceU(this string _string)
    {
        return string.IsNullOrEmpty(_string) ? _string : Regex.Replace(_string, mcIllegalOnlyCNUSAndOtherReplaceU, "_");
    }
    #endregion

    #region OnlyCNUSPAndOtherReplaceU 仅保留【字符、数字、下划线、斜杠、点】其他字符替换为下划线
    /// <summary>
    /// 仅保留【字符、数字、下划线、斜杠、点】
    /// </summary>
    const string mcIllegalOnlyCNUSPAndOtherReplaceU = @"[^\w\d_/\\\.]";
    /// <summary>
    /// 仅保留【字符、数字、下划线、斜杠、点】其他字符替换为下划线
    /// </summary>
    /// <param name="_string">要转换的字符</param>
    /// <returns>字符</returns>
    public static string HFOnlyCNUSPAndOtherReplaceU(this string _string)
    {
        return string.IsNullOrEmpty(_string) ? _string : Regex.Replace(_string, mcIllegalOnlyCNUSPAndOtherReplaceU, "_");
    }
    #endregion

    #region ClearRepeatCRLF 清除重复的回车换行符只保留一个回车换行符
    /// <summary>
    /// 清除重复的回车换行符只保留一个回车换行符
    /// </summary>
    /// <param name="_string">字符串</param>
    /// <returns>字符串</returns>
    public static string HFClearRepeatCRLF(this object _string)
    {
        return _string != null ? HFClearRepeatCRLF(_string.ToString()) : string.Empty;
    }

    /// <summary>
    /// 清除重复的回车换行符只保留一个回车换行符
    /// </summary>
    /// <param name="_string">字符串</param>
    /// <returns>字符串</returns>
    public static string HFClearRepeatCRLF(this string _string)
    {
        if (!string.IsNullOrEmpty(_string))
        {
            _string = Regex.Replace(_string, @"(\r)+", "\r", RegexOptions.Compiled | RegexOptions.Multiline);
            _string = Regex.Replace(_string, @"(\n)+", "\n", RegexOptions.Compiled | RegexOptions.Multiline);
            _string = Regex.Replace(_string, @"(\t)+", "\t", RegexOptions.Compiled | RegexOptions.Multiline);
            _string = Regex.Replace(_string, @"(\r\n)+", "\r\n", RegexOptions.Compiled | RegexOptions.Multiline);
            _string = Regex.Replace(_string, @"(\r\n\t)+", "\r\n\t", RegexOptions.Compiled | RegexOptions.Multiline);
            _string = Regex.Replace(_string, @"(\r\n)+(\r\n\t)+", "\r\n\t", RegexOptions.Compiled | RegexOptions.Multiline);
            _string = Regex.Replace(_string, @"(\r\n\t)+(\r\n)+", "\r\n", RegexOptions.Compiled | RegexOptions.Multiline);
        }
        return _string;
    }
    #endregion
    
    #region ClearCRLF 清除所有回车换行符
    /// <summary>
    /// 清除所有回车换行符
    /// </summary>
    /// <param name="_string">字符串</param>
    /// <returns>字符串</returns>
    public static string HFClearCRLF(this object _string)
    {
        return _string != null ? HFClearCRLF(_string.ToString()) : string.Empty;
    }

    /// <summary>
    /// 清除所有回车换行符
    /// </summary>
    /// <param name="_str">字符串</param>
    /// <returns>字符串</returns>
    public static string HFClearCRLF(this string _string)
    {
        return string.IsNullOrEmpty(_string) ? _string : Regex.Replace(_string, @"(\r|\n)+?", "");
    }
    #endregion

    #region ToVectorX 字符串转换为Vector类型
    /// <summary>
    /// 字符串转换为Vector2类型
    /// </summary>
    /// <param name="_srcText">字符串</param>
    /// <returns>Vector2</returns>
    public static Vector2 HFToVector2(this string _srcText)
    {
        Vector2 v = Vector2.zero;
        if (!string.IsNullOrEmpty(_srcText))
        {
            string[] vs = _srcText.HFSplit(enHFSeparatorSymbol.Comma);
            if (vs.Length > 0)
            {
                v.x = float.Parse(vs[0]);
            }
            if (vs.Length > 1)
            {
                v.y = float.Parse(vs[1]);
            }
        }
        return v;
    }

    /// <summary>
    /// 字符串转换为Vector2类型
    /// </summary>
    /// <param name="_srcText">字符串</param>
    /// <returns>Vector2</returns>
    public static Vector3 HFToVector3(this string _srcText)
    {
        Vector3 v = Vector3.zero;
        if (!string.IsNullOrEmpty(_srcText))
        {
            string[] vs = _srcText.HFSplit(enHFSeparatorSymbol.Comma);
            if (vs.Length > 0)
            {
                v.x = float.Parse(vs[0]);
            }
            if (vs.Length > 1)
            {
                v.y = float.Parse(vs[1]);
            }
            if (vs.Length > 2)
            {
                v.z = float.Parse(vs[2]);
            }
        }
        return v;
    }

    /// <summary>
    /// 字符串转换为Vector2类型
    /// </summary>
    /// <param name="_srcText">字符串</param>
    /// <returns>Vector2</returns>
    public static Vector4 HFToVector4(this string _srcText)
    {
        Vector4 v = Vector4.zero;
        if (!string.IsNullOrEmpty(_srcText))
        {
            string[] vs = _srcText.HFSplit(enHFSeparatorSymbol.Comma);
            if (vs.Length > 0)
            {
                v.x = float.Parse(vs[0]);
            }
            if (vs.Length > 1)
            {
                v.y = float.Parse(vs[1]);
            }
            if (vs.Length > 2)
            {
                v.z = float.Parse(vs[2]);
            }
            if (vs.Length > 3)
            {
                v.w = float.Parse(vs[3]);
            }
        }
        return v;
    }
    #endregion
}